package p.ithorns.sample.office.pdf;

import com.microsoft.playwright.*;
import com.microsoft.playwright.options.Cookie;
import com.microsoft.playwright.options.LoadState;
import com.microsoft.playwright.options.WaitForSelectorState;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TestPlaywrite
 *
 * @author HtL
 * @date 2025/3/10 14:15
 * @since 1.0.0
 */
public class TestPlaywrite {

    public static void main(String[] args) throws Exception {
        BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions().setHeadless(false);
        String url = "http://10.20.32.60:7200/spw-app/special-work/ticket-apply/ticket-record/185?tab=2";
        try (Playwright playwright = Playwright.create();
             Browser browser = playwright .chromium() .launch(launchOptions);
        ) {
            Browser.NewContextOptions ctxOptions = new Browser.NewContextOptions();
            ctxOptions.setBaseURL("http://10.20.32.60:7200");
            ctxOptions.setExtraHTTPHeaders(getHeaders());
            BrowserContext ctx = browser.newContext(ctxOptions);

            Page page = ctx.newPage();
            List<Cookie> cookies = new ArrayList<>();
            Cookie auth = new Cookie("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiI2NTgyNzY5NjYyMTE1NzkxOTU3Iiwicm5TdHIiOiJQaVp3YkZDaW13YW5Ybmtxd25jOUhlRm5GZXNhTWJ1diJ9.VYUAU1Eq2Ofy3PBN5uMVgtoJ-GYW3Oyen55BOM03EpY");
            auth.setUrl(url);
            cookies.add(auth);
            ctx.addCookies(cookies);

            page.navigate(url);
            page.waitForLoadState(LoadState.NETWORKIDLE);
            System.out.println(ctx.cookies());

            // page.waitForSelector("body",  new Page.WaitForSelectorOptions().setState(WaitForSelectorState.VISIBLE));
            System.out.println(page.content());
            Thread.sleep(60000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    static APIRequestContext createAPIRequestContext(Playwright playwright) {
        Map<String, String> headers = new HashMap<>();
        // We set this header per GitHub guidelines.
//        headers.put("Accept", "application/vnd.github.v3+json");
        // Add authorization token to all requests.
        // Assuming personal access token available in the environment.
        headers.put("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiI1NTc1MTg5NTc2NzE3OTE2MTI2Iiwicm5TdHIiOiJQbDBOcnBxUU54WGpqMm9GN3B6aUhiOTBNOUg2aWI4aSJ9.TdTLYRIPzNv_7wnzmQhNSZGlrp7Kg-XYCMMK-2pLxB4");

        return playwright.request().newContext(new APIRequest.NewContextOptions()
                // All requests we send go to this API endpoint.
                .setBaseURL("http://10.20.32.60:7200")
                .setExtraHTTPHeaders(headers));
    }

    static Map<String, String> getHeaders() {
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiI2NjUyNjc4NTc1NjUxNzc1ODc3Iiwicm5TdHIiOiJHZ0pFSzdWV1F6WEs0NWNmcE5ZNjZLd1hoaGpFQTNLTiJ9.BoD86rywoS5rV5Rx5uoTVcpblpcZMjIBFOuor8pvguo");
        return headers;
    }

}